<?php
// +----------------------------------------------------------------------
// | ebSIG
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.ebsig.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liudaojian <liudaojian@ebsig.com>
// +----------------------------------------------------------------------

/**
 * 商品列表服务
 * @author   liudaojian <liudaojian@ebsig.com>
 * @version 1.0
 */

namespace App\Service\Goods;

use App\Models\Goods\GoodsBigCategory;

use App\Models\Goods\GoodsMidCategory;

use App\Models\Goods\GoodsSmallCategory;

use Illuminate\Support\Facades\Redis as Redis;

class GoodsCategoryService
{

    /**
     * 获取全部分类缓存数据
     * @return array|mixed
     */
    public static function getAllCache()
    {

        $redis_key = 'WDH_GOODS_CATEGORY_ALL';

        $category = Redis::get($redis_key);
        if (!$category) {

            $category = [];

            //查询大分类
            $big_category = GoodsBigCategory::where('useFlg', 1)->orderBy('sortOrder', 'asc')->get()->toArray();
            if (!$big_category) {
                return $category;
            }
            foreach($big_category as &$big) {
                $big_id = $big['bigCategoryID'];
                $category[$big_id] = [
                    'id' => $big_id,
                    'name' => $big['description'],
                    'mid' => []
                ];
            }

            //查询中分类
            $mid_category = GoodsMidCategory::where('useFlg', 1)->orderBy('sortOrder', 'asc')->get()->toArray();
            if ($mid_category) {

                foreach ($mid_category as &$mid) {
                    $big_id = $mid['bigCategoryID'];
                    $mid_id = $mid['midCategoryID'];
                    $category[$big_id]['mid'][$mid_id] = [
                        'id' => $mid_id,
                        'name' => $mid['description'],
                        'small' => []
                    ];
                }

                //查询小分类
                $small_category = GoodsSmallCategory::where('useFlg', 1)->orderBy('sortOrder', 'asc')->get()->toArray();
                if ($small_category) {
                    foreach ($small_category as &$small) {
                        $big_id = $small['bigCategoryID'];
                        $mid_id = $small['midCategoryID'];
                        $small_id = $small['smallCategoryID'];
                        $category[$big_id]['mid'][$mid_id]['small'][$small_id] = [
                            'id' => $small_id,
                            'name' => $small['description'],
                        ];
                    }
                }

            }

            Redis::setex($redis_key, 7200, json_encode($category));

        } else {
            $category = json_decode($category);
        }

        return $category;

    }

}